Encuesta Intercensal 2015

La Encuesta Intercensal se guarda en un un archivo .RData.

data <- read_sav("~/Persona.-Encuesta Intercensal 2015.sav")

save(data, 
      file = paste0(here::here(), "/Bases/Encuesta Intercensal_2015.RData"))

Se seleccionan las variables que se desean conservar para la realización de este documento y se guarda en un archivo .RData para practicidad del manejo de datos.

Posibles variables que se pueden contemplar en la migración reciente

  • EDAD
  • SEXO
  • AFRODES 
  • HLENGUA 
  • QDIALECT_INALI
  • PERTE_INDIGENA 
  • NIVACAD
  • ALFABET 
  • SITUA_CONYUGAL 
  • CONACT 
  • SITUACION_TRAB 
  • HIJOS_NAC_VIVOS

La variable mydata contiene 22 692 265 observaciones y 12 variables.

load(paste0(here::here(), "/Bases/Encuesta Intercensal_2015.RData"))

mydata <- data %>%
           select(CVE_ENT, NOM_ENT, MUN, NOM_MUN, ENT_MUN, ENT_PAIS_RES10, MUN_RES10, ENT_PAIS_RES10, EDAD, 
                  SEXO, AFRODES, HLENGUA, QDIALECT_C, QDIALECT_INALI, PERTE_INDIGENA, NIVACAD, ESCOACUM, ESCOLARI, ALFABET, 
                  SITUA_CONYUGAL, CONACT, SITUACION_TRAB, HIJOS_NAC_VIVOS, FACTOR, ESTRATO, UPM) %>%
            rename("CVE_MUN" = "ENT_MUN")

save(mydata, file = paste0(here::here(), "/Bases/01_Migracion interna_2015.RData"))

✔️A partir de aquí se pueden correr los códidos 👇.

Se carga el archivo Migracion interna_2015.RData.

load(paste0(here::here(), "/Bases/01_Migracion interna_2015.RData"))

names(mydata)
# Para fines prácticos se genera un ponderador de uno 
mydata <- mydata %>%
           select(CVE_ENT, NOM_ENT, CVE_MUN, NOM_MUN, ENT_PAIS_RES10, MUN_RES10, EDAD, FACTOR, ESTRATO, UPM) %>%
            mutate(M = 1)  %>%
             mutate(NOM_ENT = as.factor(.$CVE_ENT)) %>%
              ungroup()

Claves de entidades y municipios

Se genera un vector con el nombre de las entdades llamado estados para facilitar los filtros en el documento. 
Se genera un vector con las abreviaturas de las entidades llamado est para fines prácticos.  
Se genera un vector con las claves de los municipios.

# Claves de los estados
estados <- sjlabelled::get_labels(mydata$CVE_ENT)

nom_estados <- c( "Aguascalientes", "Baja California" ,"Baja California Sur", "Campeche", "Coahuila de Zaragoza",
                  "Colima", "Chiapas", "Chihuahua", "Ciudad de México", "Durango", 
                  "Guanajuato", "Guerrero", "Hidalgo", "Jalisco",  "México", 
                  "Michoacán de Ocampo", "Morelos", "Nayarit", "Nuevo León", "Oaxaca", 
                  "Puebla", "Querétaro", "Quintana Roo", "San Luis Potosí", "Sinaloa", 
                  "Sonora", "Tabasco", "Tamaulipas", "Tlaxcala","Veracruz de Ignacio de la Llave", 
                  "Yucatán", "Zacatecas")

est <- c("AGS", "BC", "BCS", "CAMP", "COAH", "COL", "CHIS", "CHIH", "CDMX", "DGO", "GTO", "GRO", "HGO",
         "JAL", "MEX", "MICH", "MOR", "NAY", "NL", "OAX", "PUE", "QRO", "QROO", "SLP","SIN","SON", "TAB", 
         "TAMS", "TLX", "VER", "YUC", "ZAC")

# Claves de los municipios
MUN <- readRDS(paste0(here::here(), "/Bases/municipios_2015.RDS"))
nom_municipios <- sjlabelled::get_labels(MUN$NOM_MUN) %>% as.factor()
municipios <- sjlabelled::get_labels(MUN$CVE_MUN) %>% as.factor()

# Se le asignan las etiquetas a los nombres de los estados 
levels(mydata$NOM_ENT) <- estados

Población de 5 años y más

Se identifica a la población de 5 años y más.

filter(EDAD >= 5 & EDAD != 999)'.

Pob.5ymas <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset(EDAD >= 5 & EDAD <= 130) %>%
                 filter(ENT_PAIS_RES10 %in% estados)  # Filtro del lugar de residencia dentro del país. 

Nivel estatal

Migración reciente

Se utiliza la paquetería survey para poder trabajar con la muestra del cuestionario ampliado, en la cual se selecciona a la población de 5 años y más.

options(survey.lonely.psu = "adjust")

MC <- mydata %>%
       as.data.frame() %>%
        mutate(EDAD = as.numeric(.$EDAD)) %>%
         subset(EDAD >= 5 & EDAD <= 130) %>%
          filter(ENT_PAIS_RES10 %in% estados) %>%
           svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2015/MC_estado.RDS"))

Se genera una matriz cruzada del lugar de residencia hace 5 años a nivel estatal, utilizando la función svytable de la paquetería survey.

MC <- readRDS(paste0(here::here(), "/Output/Estado/01_Migracion reciente/2015/MC_estado.RDS")) 

Migrantes <- svytable(~ENT_PAIS_RES10 + CVE_ENT, design = MC) 

La función cross_cases() de la paquetería expss se utiliza para crear tablas de contingencia cruzadas a partir de dos o más variables categóricas. Utilizando el comando weight, permite ponderar las observaciones “factores de expansión” en la tabla.

Se quita la diagonal a la matriz cruadrada con la función diag.remove() de la paquetería sna, donde esta función reemplaza los elementos de la diagonal principal de una matriz por un valor nulo o por el valor especifico.

Migrantes <- Migrantes %>%
              as.data.frame() %>%
               expss::cross_cases(CVE_ENT, ENT_PAIS_RES10, weight = Freq) %>%
                as.data.frame() %>%
                 slice(-33) %>% 
                  select(-row_labels) 

rownames(Migrantes)<- nom_estados
colnames(Migrantes) <- nom_estados

save(Migrantes, file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2015/Matriz de migracion reciente a nivel estatal 2010-2015.RData"))

wb <- createWorkbook()
addWorksheet(wb, "MREst. 2010-2015")
writeData(wb, 1, Migrantes, colNames = TRUE, rowNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2015/Matriz de migracion reciente a nivel estatal 2010-2015.xlsx"), overwrite = TRUE)

Matriz de migración reciente hace 5 años a nivel estatal, 2010 - 2015

Matriz de migración reciente 2010 - 2015
Nivel estatal
Entidad Aguascalientes Baja California Baja California Sur Campeche Coahuila de Zaragoza Colima Chiapas Chihuahua Ciudad de México Durango Guanajuato Guerrero Hidalgo Jalisco México Michoacán de Ocampo Morelos Nayarit Nuevo León Oaxaca Puebla Querétaro Quintana Roo San Luis Potosí Sinaloa Sonora Tabasco Tamaulipas Tlaxcala Veracruz de Ignacio de la Llave Yucatán Zacatecas
Aguascalientes 1127957 520 125 22 926 144 416 1036 4654 897 2455 372 537 8269 4.3e+03 1197 541 391 825 405 1091 392 251 1692 504 404 69 1077 168 646 81 8205
Baja California 837 2825292 5263 32 1462 1437 12261 2381 5699 2571 2936 7899 1354 11940 4.8e+03 10817 2139 5378 1293 7695 4934 915 444 311 26052 16316 869 467 556 4714 426 914
Baja California Sur 5 5065 584350 12 444 243 2693 641 3144 753 488 9769 405 2878 2.9e+03 1216 384 953 399 2650 1652 440 754 369 10598 1908 81 1410 274 3351 180 251
Campeche 7 549 42 766483 143 77 4190 771 1815 351 226 523 245 623 1.5e+03 654 276 70 362 791 568 138 5759 254 186 213 9567 1723 175 8277 4028 129
Coahuila de Zaragoza 310 525 152 60 2583128 157 1152 7238 2413 10484 1250 448 624 1743 2.2e+03 685 441 97 11087 1209 890 399 313 2291 833 1196 382 5447 59 3282 93 3638
Colima 124 2019 119 39 231 600651 842 285 1957 203 1145 2682 189 16357 1.9e+03 6361 165 524 275 703 379 180 219 289 772 428 229 249 159 785 93 241
Chiapas 180 4599 710 1057 831 308 4530698 1255 5662 113 1032 1076 571 2259 4.1e+03 957 533 381 1055 4798 2396 236 3737 77 754 985 6767 1406 319 4534 1241 116
Chihuahua 382 1571 257 474 5618 189 1615 3082807 2641 9886 905 2024 172 1492 2.0e+03 1021 435 523 1680 1810 1019 162 165 568 3178 2571 362 797 25 5415 374 2181
Ciudad de México 1770 3799 885 555 1898 901 4753 2368 7920712 1335 5408 11352 10877 8492 1.6e+05 7372 11001 473 7426 13014 22016 5771 3465 2015 2995 2300 2378 3267 3193 19926 1859 1738
Durango 281 2125 336 5 6833 143 325 8666 1137 1526771 296 221 473 1303 1.0e+03 246 126 465 994 220 794 352 114 376 2270 520 104 694 33 654 139 1537
Guanajuato 1657 3099 208 89 1817 603 510 1668 12158 1369 5139521 2058 1691 9236 1.3e+04 12100 1061 479 1761 1571 2845 7663 555 2659 670 683 260 2641 497 3182 335 1267
Guerrero 85 2291 1183 79 102 456 742 365 6926 175 412 3091532 469 1623 6.7e+03 3795 5992 167 296 2803 1986 226 835 248 1569 754 530 501 202 1598 96 106
Hidalgo 311 1378 215 200 713 200 774 510 29434 280 1626 1500 2452158 1983 5.2e+04 1977 975 271 2494 1161 6847 2189 668 1450 554 447 349 1897 1385 6498 113 422
Jalisco 4676 7984 2584 134 2586 8407 3756 3172 12500 1577 6907 7185 3861 6866260 9.4e+03 20575 1439 12918 3808 3802 3339 2103 1032 2843 9530 3246 801 2524 596 6269 664 7289
México 1194 4642 1544 634 2465 968 5970 3478 301603 1148 8173 17984 20429 7069 1.4e+07 15907 8536 393 3978 18110 25304 5224 2769 2630 1851 2536 2167 3875 3821 22848 1438 1028
Michoacán de Ocampo 431 3985 220 48 768 2208 1100 514 8489 499 5870 10172 336 9484 8.9e+03 4001088 772 826 403 1135 915 1299 759 286 806 755 797 915 115 2232 57 318
Morelos 115 1315 257 68 939 157 1776 379 18244 109 630 21660 1319 1147 1.4e+04 1751 1645659 182 408 1422 4783 551 621 502 261 333 269 552 423 1806 331 138
Nayarit 237 5081 445 35 216 387 1363 596 1372 517 822 2458 258 22409 1.4e+03 2607 308 999543 182 462 399 172 133 210 4427 1579 186 345 155 441 191 644
Nuevo León 552 1510 453 280 16738 538 3137 4785 7238 4216 2878 2381 4957 4700 5.2e+03 1746 682 516 4460929 3431 2181 1200 1567 27898 2239 1668 1968 29890 317 23645 851 5134
Oaxaca 94 4043 938 321 417 258 5214 1791 11668 163 655 3986 762 1745 1.3e+04 1145 1537 202 1039 3418291 5850 340 940 327 1292 885 851 1328 470 11595 469 152
Puebla 338 2869 1007 536 844 181 4357 1098 22524 380 1162 5849 3935 2779 2.2e+04 1776 4852 385 1294 11617 5383354 1099 1375 852 1153 794 2227 3449 9484 25678 487 366
Querétaro 1234 1007 177 152 2161 463 1478 1673 25935 959 13128 2966 6839 5595 2.5e+04 8293 2862 425 2363 1438 3595 1704044 1326 3402 1375 544 499 3358 495 5120 713 700
Quintana Roo 509 1577 635 4987 820 416 26636 1255 10911 557 828 3973 910 3789 7.2e+03 767 1586 302 1927 1895 3791 429 1214501 566 1166 738 14591 1240 829 16526 21732 335
San Luis Potosí 1379 400 131 206 1642 201 314 798 3033 647 2487 710 1422 3154 3.3e+03 1036 292 173 8029 619 959 2012 516 2399319 617 494 247 8848 259 2645 108 2669
Sinaloa 321 14990 3746 61 1092 437 1142 3529 3103 4718 896 9930 1201 5241 2.4e+03 1870 368 3096 1682 3541 1151 360 177 1274 2600306 8121 261 503 88 5637 153 1074
Sonora 224 12922 1092 137 962 261 2466 5587 2243 1154 1014 1814 443 3570 1.5e+03 2369 647 2123 1898 2499 910 243 308 315 19826 2472878 417 819 131 2400 246 745
Tabasco 66 646 160 3397 346 68 9804 602 2400 68 126 389 158 734 1.8e+03 607 282 11 804 1315 1623 259 4357 130 333 512 2102474 2249 107 8467 1738 20
Tamaulipas 253 636 24 296 2534 183 1673 794 2697 471 1375 556 1310 1239 2.4e+03 1077 157 38 7942 1118 1646 868 442 9131 914 416 1050 3024590 148 28112 85 500
Tlaxcala 124 564 138 67 78 58 506 275 6009 47 434 672 1213 561 6.5e+03 527 418 32 230 935 12690 268 393 199 274 167 252 279 1108794 2573 131 100
Veracruz de Ignacio de la Llave 514 4844 709 2234 2185 351 5783 6661 19801 906 1785 2116 3583 3854 1.9e+04 2347 2087 433 6127 14020 19013 1399 4889 3557 1772 1397 7845 29277 1788 7187371 1498 388
Yucatán 189 226 122 8341 405 77 3360 668 6676 109 362 394 562 851 3.9e+03 337 521 81 1316 939 1077 563 19053 82 406 405 6013 963 85 3602 1848840 64
Zacatecas 2636 685 104 13 1942 114 230 2098 1198 1851 954 224 212 4287 1.3e+03 287 142 242 1401 479 193 426 40 1637 445 441 183 424 73 940 38 1374106
Fuente: Estimaciones del CONAPO.

Gráfico dinámico

Gráfico dinámico de migración reciente a nivel estatal.

load(file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2015/Matriz de migracion reciente a nivel estatal 2010-2015.RData"))

tabla <- Migrantes %>%
          sna::diag.remove(remove.val = 0) 

names <- c("Aguascalientes", "Baja California" ,"Baja California Sur", "Campeche", "Coahuila", "Colima", 
           "Chiapas", "Chihuahua", "Ciudad de México", "Durango", "Guanajuato", "Guerrero", "Hidalgo", "Jalisco",    
           "México", "Michoacán", "Morelos", "Nayarit", "Nuevo León", "Oaxaca", "Puebla", "Querétaro", 
           "Quintana Roo", "San Luis Potosí", "Sinaloa", "Sonora", "Tabasco", "Tamaulipas", "Tlaxcala", 
           "Veracruz", "Yucatán", "Zacatecas")

# Paleta de colores
paleta <- c("#000C7D", "#00108D", "#02149C", "#1614A4", "#3012A6", "#5A0D9D", "#7A0895", "#910390", "#A7008A", "#BB0085", "#CE0080", "#DF047A", "#EE1774", "#FA2C6C", "#FD4364", "#FE595B", "#FF6F51", "#FF8445","#FF9636", "#FFA72B", "#FFB622")

p <- chorddiag(tabla, 
               groupNames = names,
               groupColors = paleta, 
               groupnamePadding = 10, 
               #height = 700, 
               #width = 700,
               margin = 120,
               groupThickness = 0.07,
               groupPadding = 3,
               groupnameFontsize = 12,
               fadeLevel = '0.1',
               tickInterval = seq(0, 500000, 10000),
               chordedgeColor = "transparent",
               showZeroTooltips = FALSE,
               showTicks = TRUE)

# Ajusta las etiquetas usando JavaScript para modificar su posición
p <- htmlwidgets::onRender(p, '
      function(el, x) {
        d3.selectAll(".group text")
          .attr("text-anchor", "middle")
          .attr("dx", "0")  
          .attr("dy", "0.75em"); 
      }
')

# Crear un contenedor div y aplicar estilos CSS para centrarlo
#p <- tags$div(style = "display: flex; justify-content: center; align-items: center;", p)

p
p %>% 
 mapview::mapshot(url = paste0(here::here(),"/images/MR5a_2015.html"))

#htmlwidgets::saveWidget(p, paste0(here::here(), "/Graficos/Estado/01_Migracion reciente/2015/MR5a a nivel estatal 2010 - 2015.html"), selfcontained = TRUE)
#require(webshot)
#webshot(url = paste0(here::here(), "/Graficos/Estado/01_Migracion reciente/2015/MR5a a nivel estatal 2010 - 2015.html"),
 #         file = paste0(here::here(), "/Graficos/Estado/01_Migracion reciente/2015/MR5a a nivel estatal 2010 - 2015.png"),
  #                cliprect = "viewport")

Gráficos migración reciente

ChordDiagram

paleta <- c("#000C7D", "#00108D", "#02149C", "#1614A4", "#3012A6", "#5A0D9D", "#7A0895", "#910390", "#A7008A", "#BB0085", "#CE0080", "#DF047A", "#EE1774", "#FA2C6C", "#FD4364", "#FE595B", "#FF6F51", "#FF8445","#FF9636", "#FFA72B", "#FFB622")

load(file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2015/Matriz de migracion reciente a nivel estatal 2010-2015.RData"))

tabla <- Migrantes %>%
          sna::diag.remove(remove.val = 0)  

rownames(tabla) <- stringr::str_wrap(nom_estados, 50)
colnames(tabla) <- stringr::str_wrap(nom_estados, 50)

# Paleta de colores
tabla2 <- color_chord_diagram(tabla1 = tabla, paleta)
#svglite::svglite(paste0(here::here(), "/Graficos/Estado/01_Migracion reciente/2015/ChordDiagram de MR5a a nivel estatal.svg"), width = 20, height = 20)
file = "/Graficos/Estado/01_Migracion reciente/2015/ChordDiagram de MR5a a nivel estatal.pdf"

## Gráficos a nivel estatal 
chord_diagram_graph(file = file, 
                    width = 7, 
                    height = 7, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla, 
                    tabla2 = tabla2, 
                    color_labels = "#000C7D",
                    transparency = 0.2,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text =c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Gráficos por estados

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

load(file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2015/Matriz de migracion reciente a nivel estatal 2010-2015.RData"))

tabla <- Migrantes %>%
          sna::diag.remove(remove.val = 0) 

rownames(tabla) <- stringr::str_wrap(nom_estados, 100)
colnames(tabla) <- stringr::str_wrap(nom_estados, 100)

# Nombre de los estados 
estado <- stringr::str_wrap(nom_estados, 100)

filtro_est <- Migrantes %>%
               as.data.frame() %>%
                tibble::rownames_to_column(var = "rn") %>% 
                 melt(., id.vars = "rn", variable.name = "cn") %>%
                  mutate_if(is.factor, as.character) 

### Sacar el promedio de los flujos migratiorios para determinar como se van a grupar los estados 
#### Es importante correr la tabla1[[x]] sin filtros para determinar el número promedio de flujos de migración
#### Filtro <<<<< filter(value > 0 & rn != estado[x]) %>%
#tabla_estados <- sapply(1:32, function(x)
 #                       mean(tail(sort(tabla1[[x]]), 1), na.rm = TRUE))
#p <- data.frame(estados = est,
 #               filtro_estados = filtro_mig)
#write.table(p, file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2015/Filtro a nivel estatal.txt"))
#write.xlsx(p, file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2015/Filtro a nivel estatal.xlsx"))

filtro_mig <- read.xlsx(paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2015/Filtro a nivel estatal.xlsx"), colNames = TRUE) %>% 
               pull(filtro_estados)

tabla1 <- migration_flows_states(tabla = tabla, 
                                 filtro_mig = filtro_mig, 
                                 filtro_est = filtro_est, 
                                 category = estado, 
                                 group = "Otro estados")

## Se guardan las matrices de migración reciente para analizarlos después. 
wb <- createWorkbook()
for(i in 1:32){
     tabla <- tabla1[[i]] %>%
                as.data.frame() %>%
                 adorn_totals(c("row", "col"), 
                               fill = "-", 
                                na.rm = TRUE, 
                                 name = "Total",,,,contains(colnames(tabla1[[i]])))
                 
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, tabla, colNames = TRUE, rowNames = TRUE)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2015/Matriz MR5a por estados_2010-2015_Reduccion.xlsx"), 
               overwrite = TRUE)
}
saveRDS(tabla1, file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2015/Tabla MR5a a por estados.RDS"))
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2015/Tabla MR5a a por estados.RDS"))

total_tablas <- totales(tabla1 = tabla1, 
                        Clave = "CVE_ENT", 
                        Inmigrantes = "Inmigrantes",  
                        Emigrantes = "Emigrantes")

porcentajes_tablas <- porcentajes(tabla1 = tabla1, 
                                  Clave = "CVE_ENT", 
                                  Inmigrantes = "%Inmigrantes",  
                                  Emigrantes = "%Emigrantes")

# Se guardan los totales de las matrices reducidas 
wb <- createWorkbook()
for(i in 1:32){
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, total_tablas[[i]], colNames = TRUE, startCol = 1)
     writeData(wb, i, porcentajes_tablas[[i]], colNames = TRUE, startCol = 5)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2015/Matriz MR5a por estados_2010-2015_Reduccion_Totales.xlsx"), 
               overwrite = TRUE)
}
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2015/Tabla MR5a a por estados.RDS"))

#paleta <- colorRampPalette(pals::kovesi.linear_bmy_10_95_c78(100))(50)
paleta <- c("#000C7D", "#00108D", "#02149C", "#1614A4", "#3012A6", "#5A0D9D", "#7A0895", "#910390", "#A7008A", "#BB0085", "#CE0080", "#DF047A", "#EE1774", "#FA2C6C", "#FD4364", "#FE595B", "#FF6F51", "#FF8445","#FF9636")

tabla2 <- color_chord_diagram(tabla1 = tabla1, paleta)
file = "/Graficos/Estado/01_Migracion reciente/2015/ChordDiagram de MR5a desagregado por estado.pdf"

## Gráficos a nivel estatal 
chord_diagram_graph(file = file, 
                    width = 8, 
                    height = 8, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#000C7D",
                    transparency = 0,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text =c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Etiquetas

file = "/Graficos/Estado/01_Migracion reciente/2015/Etiquetas a nivel estatal.pdf"

labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 8, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = nom_estados)

Gráfico Sankey

load(file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2015/Matriz de migracion reciente a nivel estatal 2010-2015.RData"))

Migrantes <- Migrantes %>%
              sna::diag.remove(remove.val = 0)  

rownames(Migrantes) <- stringr::str_wrap(nom_estados, 50)
colnames(Migrantes) <- stringr::str_wrap(nom_estados, 50)

# Matiz migración interna (Población de 5 años y más)
tabla <- Migrantes %>% 
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             as_tibble() %>%
              mutate(rn = forcats::fct_relevel(.$rn, nom_estados),
                     cn = forcats::fct_relevel(.$cn, nom_estados)) %>%
               filter(value >= 0)  
p <- tabla %>% 
       ggplot(aes(axis1 = rn, 
                   axis2 = cn, 
                    y = value),  # c("value", "freq", "tasa")
               reverse = FALSE, 
                na.rm = TRUE) +
        geom_alluvium(aes(fill = rn),
                       curve_type = "quintic", 
                        color = "transparent", 
                         alpha = 0.85, 
                          lwd = 0.001, 
                           width = 1/5,
                            reverse = FALSE) +
         geom_stratum(aes(fill = cn), 
                       color = "white", 
                        alpha = 0.65,  
                         lwd = 0.001, 
                          width = 1/5,
                           reverse = FALSE) +
          geom_text_repel(aes(label = ifelse(after_stat(x) == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""), 
                               fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                           stat = "stratum", 
                            size = 3, 
                             direction = "y", 
                              nudge_x = -.3,
                               min.segment.length = unit(1, "lines"),
                                force = 1,
                                 force_pull = 0,
                                  family = "montserrat",
                                   reverse = FALSE) +
           geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                               fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                            stat = "stratum", 
                             size = 3,
                              direction = "y", 
                               nudge_x = .3, 
                                force = 1,
                                 force_pull = 0,
                                  family = "montserrat",
                                   reverse = FALSE) +
            theme_void() + 
             theme(plot.margin = margin(t = 1, r = 1, b = 1, l = 1, "cm"),
                    text = element_text(family = "montserrat"),
                     axis.text = element_blank(),
                      axis.title = element_blank(),
                       strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                        legend.key.size = unit(0.5, "cm"),
                         legend.text = element_text(size = 9, family = "montserrat")) + 
              scale_x_discrete(expand = c(0, 0.4)) +
               scale_fill_viridis_d(option = "A", end = 0.9, begin = 0.2) +
                guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                 labs(fill = "", 
                       color = "")
p
path = paste0(here::here(), "/Graficos/Estado/01_Migracion reciente/2015/GSankey de MR5a a nivel estatal.pdf")
ggexport(p, width = 14, height = 10, dpi = 400, filename = path)

Desagregado por estados

load(file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2015/Matriz de migracion reciente a nivel estatal 2010-2015.RData"))

# Se cambian las equiquetas de los estados 
estados <- est 

rownames(Migrantes) <- estados
colnames(Migrantes) <- estados

tabla <- Migrantes %>%
          sna::diag.remove(remove.val = 0) %>%
           as.data.frame() %>%
            tibble::rownames_to_column(var = "rn") %>% 
             melt(., id.vars = "rn", variable.name = "cn") %>%
              mutate_if(is.factor, as.character) %>%
               mutate(value = ifelse((.$rn != .$cn) & (.$rn %in% estados | .$cn %in% estados), value, 0)) %>%
                mutate(rn = forcats::fct_relevel(.$rn, estados),
                       cn = forcats::fct_relevel(.$cn, estados)) 
p <- lapply(1:32, function(x){
                   tabla <- tabla %>%
                             mutate(rn = forcats::fct_relevel(.$rn, estados),
                                    cn = forcats::fct_relevel(.$cn, estados)) %>%
                              mutate(value = ifelse(.$rn %in% estados[x] | .$cn %in% estados[x], value, 0)) 

                    tabla %>% 
                     ggplot(aes(axis1 = rn, 
                                 axis2 = cn, 
                                  y = value),  # c("value", "freq", "tasa")
                             reverse = FALSE, 
                              na.rm = TRUE) + 
                      geom_alluvium(aes(fill = rn),  
                                     color = "transparent", 
                                      alpha = 0.8, 
                                       lwd = 0.001, 
                                        width = 1/5,
                                         reverse = FALSE) +
                       geom_stratum(aes(fill = rn), 
                                     color = "#F1F1F1", 
                                      alpha = 1, 
                                       lwd = 0.001, 
                                        width = 1/5,
                                         reverse = FALSE) +
                         geom_text_repel(aes(label = ifelse(after_stat(x)  == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                             fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                                           stat = "stratum", 
                                            size = 3,
                                             direction = "y", 
                                              nudge_x = -.2, 
                                               force = 1,
                                                        force_pull = 0,
                                                         family = "montserrat",
                                                          reverse = FALSE) +
                          geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                              fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                                           stat = "stratum", 
                                            size = 3,
                                             direction = "y", 
                                              nudge_x = .2, 
                                               force = 1,
                                                force_pull = 0,
                                                 family = "montserrat",
                                                  reverse = FALSE) +
                            theme_void() + 
                             theme(plot.margin = margin(t = 1, r = 1.5, b = 1, l = 0, "cm"),
                                    text = element_text(family = "montserrat"),
                                     axis.text = element_blank(),
                                      axis.title = element_blank(),
                                       strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                                        legend.key.size = unit(0.5, "cm"),
                                         legend.text = element_text(size = 9, family = "montserrat"),
                                          legend.position = c(1, .5)) + 
                              scale_x_discrete(expand = c(-0.1, 0.35)) +
                               scale_fill_viridis_d(option = "A", end = 0.9, begin = 0.2) +
                                guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                                 labs(fill = "", 
                                      color = "")
              }
        )

path = paste0(here::here(), "/Graficos/Estado/01_Migracion reciente/2015/GSankey de MR5a desagregado por estados_Absolutos.pdf")
ggexport(list = p, width = 14, height = 10, dpi = 400, filename = path)

Indicadores

Se realizan cálculos generales de migración

  • Residentes

  • Inmigrantes

  • Emigrantes

  • % Inmigrantes

  • % Emigrante

  • Migración bruta

  • Migración Neta

  • % Tasa de migración bruta

  • % Tasa de migración neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_ENT) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
###################### Población de 5 años y más ###############################
Pob.5ymas <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset(EDAD >= 5 & EDAD <=130) %>%
                 group_by(CVE_ENT) %>%
                  summarise(Pob.5ymas = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(file = paste0(here::here(), "/Bases/Estado/01_Migracion reciente/2015/Matriz de migracion reciente a nivel estatal 2010-2015.RData"))

rownames(Migrantes) <- estados
colnames(Migrantes) <- estados
  
Residentes <- Migrantes %>%
               rownames_to_column() %>%
                tidyr::gather(CVE_ENT, Value, -rowname)%>%
                 filter(rowname == CVE_ENT) %>%
                  select(-rowname) %>%
                   droplevels() %>%
                    rename("Residentes" = "Value") 

################################################################################
############################### Inmigrantes ####################################
Inmigrantes <- Migrantes %>% 
                sna::diag.remove(remove.val = 0) %>%
                 as.data.frame() %>%
                  tibble::rownames_to_column(var = "CVE_ENT") %>%
                   melt(., id.vars = "CVE_ENT", variable.name = "ENT_PAIS_RES10") %>%
                    mutate_at(vars(3), as.numeric) %>%
                     as_tibble() %>%
                      filter(CVE_ENT != ENT_PAIS_RES10) %>%
                       group_by(CVE_ENT) %>%
                        summarise(Inmigrantes = sum(value, na.rm = TRUE))

################################################################################
############################### Emigrantes #####################################
Emigrantes <- Migrantes %>% 
               sna::diag.remove(remove.val = 0) %>%
                as.data.frame() %>%
                 tibble::rownames_to_column(var = "CVE_ENT") %>%
                  melt(., id.vars = "CVE_ENT", variable.name = "ENT_PAIS_RES10") %>%
                   mutate_at(vars(3), as.numeric) %>%
                    as_tibble() %>%
                     filter(CVE_ENT != ENT_PAIS_RES10) %>%
                      group_by(ENT_PAIS_RES10) %>%
                       summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
                        rename("CVE_ENT" = "ENT_PAIS_RES10") 

tabla <- Pob.Total %>%
          left_join(., Pob.5ymas, by = c("CVE_ENT")) %>%
          left_join(., Residentes, by = c("CVE_ENT")) %>%
          left_join(., Inmigrantes, by = c("CVE_ENT")) %>%
          left_join(., Emigrantes, by = c("CVE_ENT")) %>%
           mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                  Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                  Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2))*1000,
                  Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2))*1000,
                  Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                  Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Estado/01_Migracion reciente/2015/Indicadores de MR5a por estado 2010 - 2015.xlsx"), overwrite = TRUE)
save(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Indicadores de MR5a por estado 2010 - 2015.RData"))
Indicadores de migración reciente
Nivel estatal
Clave de Entidad Pob.Total Pob.5ymas Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
001 1 312 544 1 184 416 1 127 957 42 569 21 035 21 534 63 604 6.8 3.4 3.4 −42 070
002 3 315 766 3 033 475 2 825 292 145 082 97 466 47 616 242 548 9.1 6.1 3.0 −194 932
003 712 029 649 033 584 350 56 340 23 981 32 359 80 321 16.6 7.0 9.5 −47 962
004 899 931 816 326 766 483 44 192 24 571 19 621 68 763 10.3 5.7 4.6 −49 142
005 2 954 915 2 678 545 2 583 128 61 110 60 158 952 121 268 4.3 4.3 0.1 −120 316
006 711 235 649 503 600 651 40 115 20 591 19 524 60 706 11.8 6.1 5.7 −41 182
007 5 217 908 4 629 951 4 530 698 54 062 110 338 −56 276 164 400 2.2 4.5 −2.3 −220 676
008 3 556 574 3 239 718 3 082 807 51 550 66 937 −15 387 118 487 3.0 3.9 −0.9 −133 874
009 8 918 653 8 365 831 7 920 712 322 828 545 284 −222 456 868 112 7.5 12.6 −5.1 −1 090 568
010 1 754 754 1 579 267 1 526 771 32 786 48 513 −15 727 81 299 3.9 5.8 −1.9 −97 026
011 5 853 677 5 302 301 5 139 521 89 071 68 665 20 406 157 736 3.2 2.5 0.7 −137 330
012 3 533 251 3 174 481 3 091 532 43 336 135 343 −92 007 178 679 2.6 8.1 −5.5 −270 686
013 2 858 359 2 606 469 2 452 158 120 625 71 317 49 308 191 942 8.8 5.2 3.6 −142 634
014 7 844 830 7 132 509 6 866 260 157 507 150 406 7 101 307 913 4.2 4.0 0.2 −300 812
015 16 187 608 14 833 673 14 154 329 499 716 401 383 98 333 901 099 6.4 5.2 1.3 −802 766
016 4 584 471 4 135 938 4 001 088 65 395 113 422 −48 027 178 817 3.0 5.2 −2.2 −226 844
017 1 903 811 1 744 832 1 645 659 76 240 51 557 24 683 127 797 8.4 5.7 2.7 −103 114
018 1 181 050 1 065 776 999 543 50 084 32 550 17 534 82 634 8.9 5.8 3.1 −65 100
019 5 119 504 4 682 806 4 460 929 164 447 74 778 89 669 239 225 6.7 3.1 3.7 −149 556
020 3 967 889 3 595 317 3 418 291 73 251 107 607 −34 356 180 858 3.9 5.7 −1.8 −215 214
021 6 168 883 5 586 559 5 383 354 136 296 136 836 −540 273 132 4.6 4.7 0.0 −273 672
022 2 038 372 1 853 286 1 704 044 125 469 37 878 87 591 163 347 12.9 3.9 9.0 −75 756
023 1 501 562 1 364 091 1 214 501 133 392 57 976 75 416 191 368 18.6 8.1 10.5 −115 952
024 2 717 820 2 478 417 2 399 319 49 333 68 440 −19 107 117 773 3.8 5.3 −1.5 −136 880
025 2 966 321 2 710 831 2 600 306 82 145 99 622 −17 477 181 767 5.8 7.0 −1.2 −199 244
026 2 850 330 2 596 409 2 472 878 71 251 53 756 17 495 125 007 5.2 3.9 1.3 −107 512
027 2 395 272 2 166 578 2 102 474 43 578 62 571 −18 993 106 149 3.8 5.5 −1.7 −125 142
028 3 441 698 3 136 127 3 024 590 70 087 112 414 −42 327 182 501 4.3 6.8 −2.6 −224 828
029 1 272 847 1 157 531 1 108 794 36 692 26 429 10 263 63 121 6.0 4.3 1.7 −52 858
030 8 112 505 7 420 636 7 187 371 172 101 233 398 −61 297 405 499 4.4 6.0 −1.6 −466 796
031 2 097 175 1 924 250 1 848 840 61 701 39 988 21 713 101 689 6.1 4.0 2.2 −79 976
032 1 579 209 1 422 963 1 374 106 25 268 42 409 −17 141 67 677 3.4 5.7 −2.3 −84 818
Fuente: Estimaciones del CONAPO.

Nivel municipal

Migración reciente

Matrices

#Clave de los municipios 2015 
municipios <- MUN %>%
               select(CVE_MUN) %>%
                unique() %>%
                 pull(CVE_MUN)

Pob.5ymas <- mydata %>%
              mutate(CVE_MUN_RES = paste0(ENT_PAIS_RES10, MUN_RES10)) %>%
               mutate(ENT_PAIS_RES10 = case_when(.$ENT_PAIS_RES10 %in% estados ~.$ENT_PAIS_RES10,
                                                 .$ENT_PAIS_RES10 %nin% estados ~ "888", #Residencia en otro país
                                                 .$ENT_PAIS_RES10 %in% "997" ~ "997",
                                                 .$ENT_PAIS_RES10 %in% "998" ~ "998",
                                                 .$ENT_PAIS_RES10 %in% "997" ~ "999"),
                      CVE_MUN_RES = case_when(.$CVE_MUN_RES %in% municipios ~.$CVE_MUN_RES,
                                               nchar(.$CVE_MUN_RES) == 3 ~ "888",  #Residencia en otro país
                                              .$CVE_MUN_RES %in% "997999" ~ '997999',
                                              .$ENT_PAIS_RES10 %in% "997" ~ "997",
                                              .$ENT_PAIS_RES10 %in% "998" ~ "998",
                                              .$ENT_PAIS_RES10 %in% "999" ~ "999",
                                              .$MUN_RES10 %in% "999" ~ "999")) %>%
                 mutate(EDAD = as.numeric(.$EDAD)) %>%
                  subset(EDAD >= 5 & EDAD <= 130) %>%
                   select(FACTOR, ESTRATO, UPM, CVE_MUN, CVE_MUN_RES, EDAD) %>%
                    filter(CVE_MUN_RES %in% municipios) 

MC <- Pob.5ymas %>%
       svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/MC_municipal.RDS"))
MC <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/MC_municipal.RDS"))

Migrantes <- svytable(~CVE_MUN_RES + CVE_MUN, design = MC) 

Se genera la matriz cuadrada y se le asignan los nombres de los estados.

Migrantes <- Migrantes %>%
              as.data.frame() %>%
               expss::cross_cases(CVE_MUN, CVE_MUN_RES, weight = Freq) %>%
                as.data.frame() %>%
                 rename("CVE_MUN" = "row_labels") %>% 
                  arrange(CVE_MUN) %>%
                   slice(-1) 

rownames <- Migrantes %>% 
             mutate(CVE_MUN = substr(.$CVE_MUN, 9, 16)) %>% 
              pull(CVE_MUN)

colnames <- names(Migrantes) %>% 
             as.data.frame() %>% 
              slice(-1) %>% 
               rename("CVE_MUN" = ".") %>%
                mutate(`CVE_MUN` = substr(.$CVE_MUN, 13, 20)) %>%
                 pull(CVE_MUN)

# Se elimina la variable CVE_MUN
Migrantes <- Migrantes %>%
              select(-CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

saveRDS(Migrantes, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Matriz de migracion reciente a nivel municipal.RDS"))
save(Migrantes, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Matriz de migracion reciente a nivel municipal.RData"))

require(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "M.Reciente")
writeData(wb, 1, Migrantes %>% as.data.frame() %>% tibble::rownames_to_column(var = "CVE_MUN"), colNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2015/Matriz de migracion reciente a nivel municipal.xlsx"), overwrite = TRUE)

Matriz de migración reciente hace 5 años a nivel municipal, 2010 - 2015

Matriz de migración reciente 2010 - 2015
Nivel municipal
CVE_MUN 001001 001002 001003 001004 001005 001006 001007 001008 001009 001010 001011 002001 002002 002003 002004 002005 003001 003002 003003 003008 003009 004001 004002 004003 004004 004005 004006 004007 004008
001001 752398 1045 525 90 991 105 142 26 76 237 448 45 69 0 213 0 0 0 0 86 0 0 20 0 0 0 0 0 0
001002 221 39638 3 3 12 88 9 0 40 4 57 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001003 157 0 47892 5 16 12 11 0 0 0 12 5 0 8 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001004 73 2 4 13268 24 0 69 1 6 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001005 6154 43 52 115 94341 76 136 34 0 0 829 0 60 0 25 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001006 274 46 6 18 78 39191 161 58 135 0 369 0 3 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001007 84 43 20 136 19 143 45817 9 191 0 78 0 4 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001008 87 1 5 2 5 20 49 7407 1 0 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001009 70 61 0 2 13 77 171 1 17496 0 53 0 0 2 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001010 224 74 0 0 2 0 4 0 2 17285 12 0 0 0 2 0 0 0 0 2 0 0 0 0 0 0 0 0 0
001011 3910 116 24 54 792 581 177 9 61 32 32515 4 0 0 7 0 0 0 3 0 0 0 0 0 0 0 0 0 0
002001 35 0 0 0 0 0 0 0 0 0 0 412427 964 353 1437 52 163 1573 489 251 0 0 0 32 0 0 0 0 0
002002 130 0 0 0 0 0 0 0 0 0 0 1560 859462 343 1801 171 98 106 183 190 0 0 0 0 0 0 0 0 0
002003 12 0 0 0 0 0 0 0 0 0 0 177 543 85149 657 213 0 18 24 22 0 0 0 0 0 0 0 0 0
002004 660 0 0 0 0 0 0 0 0 0 0 1623 1620 1658 1373161 987 80 25 589 289 0 0 0 0 0 0 0 0 0
002005 0 0 0 0 0 0 0 0 0 0 0 264 350 130 2819 77081 0 0 49 22 0 0 0 0 0 0 0 0 0
003001 0 0 0 0 0 0 0 0 0 0 0 132 94 0 205 12 61838 244 1019 633 357 0 0 0 0 0 0 0 0
003002 0 0 0 0 0 0 0 0 0 0 0 787 88 5 168 35 73 47231 358 87 107 0 0 0 0 0 0 0 0
003003 0 0 0 0 0 0 0 0 0 0 0 553 133 43 211 60 1266 1440 230464 2758 328 0 4 0 0 0 0 0 0
003008 5 0 0 0 0 0 0 0 0 0 0 266 297 6 587 730 663 114 1336 217850 99 0 6 0 0 0 0 0 0
003009 0 0 0 0 0 0 0 0 0 0 0 66 24 0 30 0 136 86 202 148 15349 0 0 2 0 0 0 0 0
004001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 50623 139 61 59 24 8 0 4
004002 4 0 0 0 0 0 0 0 0 0 0 5 3 0 0 0 0 0 0 0 0 278 244921 749 904 125 537 86 38
004003 0 0 0 0 0 0 0 0 0 0 0 308 0 39 46 0 0 0 0 9 0 204 550 198216 288 107 3 153 11
004004 0 0 0 0 0 0 0 0 0 0 0 21 6 0 33 0 0 0 0 0 0 73 662 386 75323 17 23 31 0
004005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 28 151 47 38 27235 54 3 17
004006 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 42 273 39 18 23 34135 0 3
004007 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 22 107 3 2 8 7757 1
004008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 4 0 0 0 0 0 13 140 9 21 14 4 10 9316
004009 0 0 0 0 0 0 0 0 0 0 0 0 1 0 4 0 0 0 0 0 0 46 350 307 130 20 12 11 6
Fuente: Estimaciones del CONAPO.

Gráficos

Gráficos por municipios

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

# Matriz cuadrada a nivel municipal 
load(paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Matriz de migracion reciente a nivel municipal.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

# Nombre de los estados 
estado <- stringr::str_wrap(nom_estados, 100)

municipios <- MUN %>% 
               mutate(municipios = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                pull(municipios)

################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(municipios, Migrantes)

Emigrantes <- Emigrantes_function(municipios, Migrantes)

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE))

filtro_est <- Migrantes %>%
               as.data.frame() %>%
                tibble::rownames_to_column(var = "rn") %>% 
                 melt(., id.vars = "rn", variable.name = "cn") %>%
                  mutate_if(is.factor, as.character) %>%
                   filter(value > 0)

### Sacar el promedio de los flujos migratiorios para determinar como se van a grupar los estados 
#### Es importante correr la tabla1[[x]] sin filtros para determinar el número promedio de flujos de migración
#### Filtro <<<<< filter(value > 0 & rn != estado[x]) %>%
#### Se anexa el filtro de estados filter(value > 100000000 & rn != estado[x]) %>% 
#### De esta manera solo se contemplan a los municipios

#p <- data.frame(estados = est,
 #               filtro_municipio = filtro_mig,
  #              filtro_estado = filtro_out)
#write.xlsx(p, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2015/Filtro a nivel municipal.xlsx"), overwrite = TRUE)

#### Filtro de municipios
filtro_mig <- read.xlsx(paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2015/Filtro a nivel municipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_municipio)

#### Filtro de estados 
filtro_out <- read.xlsx(paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2015/Filtro a nivel municipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_estado)

tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character) %>%
              filter(value > 0)

## Se generan los filtros correspondientes a la matriz cuadrada por estados 
tabla1 <- migration_flows_municipality(tabla = tabla, 
                                       filtro_mun = filtro,
                                       filtro_est = filtro_est,
                                       filtro_mig = filtro_mig, 
                                       filtro_out = filtro_out, 
                                       category_group = estados, 
                                       category_names = nom_estados,
                                       group_mun = "Otros municipios",
                                       group_est = "Otros estados")

## Se sacan los flujos migratorios que pertencen a otros estados
#tabla_estados <- sapply(1:32, function(i){
#                         tabla1[[i]] %>%
#                          as.data.frame() %>%
#                           adorn_totals(c("row", "col"), 
#                                         fill = "-", 
#                                          na.rm = TRUE, 
#                                           ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                            select(`Otros estados`) %>%
#                             slice(nrow(.)) %>%
#                              mutate(`Otros estados` = .$`Otros estados`/30) %>%
#                               pull(`Otros estados`)
#  }
#)

## Se sacan los flujos migratorios que pertencen a otros municipios
#tabla_municipios <- sapply(1:32, function(i){
#                               tabla1[[i]] %>%
#                                as.data.frame() %>%
#                                 select(-c(`Otros estados`)) %>%
#                                  adorn_totals(c("row", "col"), 
#                                                fill = "-", 
#                                                 na.rm = TRUE, 
#                                                  ,,,,contains(colnames(tabla1[[i]])))  N%>% 
#                                    slice(nrow(.)) %>%
#                                     mutate(Total = .$Total/50) %>%
#                                      pull(Total)
#})
## Se guardan las matrices de movilidad migración reciente para analizarlos después. 
wb <- createWorkbook()
for(i in 1:32){
     tabla <- tabla1[[i]] %>%
                as.data.frame() %>%
                 adorn_totals(c("row", "col"), 
                               fill = "-", 
                                na.rm = TRUE, 
          ,,,,contains(colnames(tabla1[[i]])))
                 
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, tabla, colNames = TRUE, rowNames = TRUE)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Matriz MR5a nivel municipal_Reduccion.xlsx"), 
               overwrite = TRUE)
}

saveRDS(tabla1, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Tabla MR5a a nivel municipal.RDS"))
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Tabla MR5a a nivel municipal.RDS"))

total_tablas <- totales(tabla1 = tabla1, 
                        Clave = "CVE_MUN", 
                        Inmigrantes = "Inmigrantes",  
                        Emigrantes = "Emigrantes")

porcentajes_tablas <- porcentajes(tabla1 = tabla1, 
                                  Clave = "CVE_MUN", 
                                  Inmigrantes = "%Inmigrantes",  
                                  Emigrantes = "%Emigrantes")
# Se guardan los totales de las matrices reducidas 
wb <- createWorkbook()
for(i in 1:32){
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, total_tablas[[i]], colNames = TRUE, startCol = 1)
     writeData(wb, i, porcentajes_tablas[[i]], colNames = TRUE, startCol = 5)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Matriz MR5a nivel municipal_Reduccion_Totales.xlsx"), 
               overwrite = TRUE)
}

Dada la magnitud de municipios en algunos estados se seleccionan solo algunos de ellos.

tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Tabla MR5a a nivel municipal.RDS"))

## Paleta de colores
#paleta <- colorRampPalette(pals::kovesi.linear_bmy_10_95_c78(100))(50)
paleta <- c("#000C7D", "#00108D", "#02149C", "#1614A4", "#3012A6", "#5A0D9D", "#7A0895", "#910390", "#A7008A", "#BB0085", "#CE0080", "#DF047A", "#EE1774", "#FA2C6C", "#FD4364", "#FE595B", "#FF6F51", "#FF8445","#FF9636", "#FFA72B", "#FFB622")

tabla2 <- color_chord_diagram(tabla1 = tabla1, paleta)
file = "/Graficos/Municipio/01_Migracion reciente/2015/ChordDiagram de MR5a desagregado por estado.pdf"

## Gráficos a nivel municipal
chord_diagram_graph(file = file, 
                    width = 15, 
                    height = 10, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#000C7D",
                    transparency = 0,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text =c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Etiquetas

file =  "/Graficos/Municipio/01_Migracion reciente/2015/Etiquetas a nivel municipal.pdf"

labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 10, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = nom_estados)

Indicadores

Se realizan cálculos generales de migración
- Residentes
- Inmigrantes
- Emigrantes
- % Inmigrantes
- % Emigrante
- Migración Bruta
- Migración Neta
- % Tasa de migración bruta
- % Tasa de migración neta

Dada la cantidad de datos a calcular para la obtención de los indicadores. La obtención de los indicadores se realiza de manera directa, es decir, partiendo de la matriz cuadrada original.

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_MUN) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
###################### Población de 5 años y más ###############################
Pob.5ymas <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset(EDAD >= 5 & EDAD <=130) %>%
                 group_by(CVE_MUN) %>%
                  summarise(Pob.5ymas = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Matriz de migracion reciente a nivel municipal.RData"))

Residentes <- Migrantes %>%
               rownames_to_column() %>% 
                gather(CVE_MUN, Value, -rowname)%>%
                 filter(rowname == CVE_MUN) %>%
                  select(-rowname) %>%
                   droplevels() %>%
                    rename("Residentes" = "Value") 

################################################################################
############################### Inmigrantes ####################################
Inmigrantes <- Migrantes %>% 
                as.data.frame() %>%
                 tibble::rownames_to_column(var = "CVE_MUN") %>%
                  melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
                   mutate_at(vars(3), as.numeric) %>%
                    as_tibble() %>%
                     filter(CVE_MUN != CVE_MUN_RES) %>%
                      group_by(CVE_MUN) %>%
                       summarise(Inmigrantes = sum(value, na.rm = TRUE))

################################################################################
############################### Emigrantes #####################################
Emigrantes <- Migrantes %>% 
               as.data.frame() %>%
                tibble::rownames_to_column(var = "CVE_MUN") %>%
                 melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
                  mutate_at(vars(3), as.numeric) %>%
                   as_tibble() %>%
                    filter(CVE_MUN != CVE_MUN_RES) %>%
                     group_by(CVE_MUN_RES) %>%
                      summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
                       rename("CVE_MUN" = "CVE_MUN_RES") 

tabla <- Pob.Total %>%
          left_join(., Pob.5ymas, by = c("CVE_MUN")) %>%
          left_join(., Residentes, by = c("CVE_MUN")) %>%
          left_join(., Inmigrantes, by = c("CVE_MUN")) %>%
          left_join(., Emigrantes, by = c("CVE_MUN")) %>%
            mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                   Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                   Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2))*1000,
                   Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2))*1000,
                   Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                   Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Indicadores de migracion reciente a nivel municipal 2010 - 2015.xlsx"), overwrite = TRUE)

save(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Indicadores de migracion reciente a nivel municipal 2010 - 2015.RData"))
Indicadores de migración reciente
Nivel municipal
CVE_MUN Pob.Total Pob.5ymas Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
001001 877 190 797 086 752 398 29 265 29 925 −660 59 190 7.0 7.1 −0.2 −59 850
001002 46 464 41 358 39 638 1 103 1 612 −509 2 715 5.0 7.3 −2.3 −3 224
001003 56 048 50 160 47 892 763 1 047 −284 1 810 2.9 3.9 −1.1 −2 094
001004 15 577 13 875 13 268 427 536 −109 963 5.8 7.3 −1.5 −1 072
001005 120 405 107 447 94 341 10 984 2 158 8 826 13 142 19.3 3.8 15.5 −4 316
001006 46 473 41 391 39 191 1 662 1 200 462 2 862 7.6 5.5 2.1 −2 400
001007 53 866 47 805 45 817 1 217 1 223 −6 2 440 4.8 4.8 0.0 −2 446
001008 8 896 7 811 7 407 244 248 −4 492 5.8 5.9 −0.1 −496
001009 20 926 18 568 17 496 751 540 211 1 291 7.6 5.5 2.1 −1 080
001010 20 245 18 101 17 285 582 306 276 888 6.1 3.2 2.9 −612
001011 46 454 40 814 32 515 7 392 2 015 5 377 9 407 33.9 9.2 24.6 −4 030
002001 486 639 444 765 412 427 21 658 15 918 5 740 37 576 9.3 6.8 2.5 −31 836
002002 988 417 906 626 859 462 30 442 19 644 10 798 50 086 6.4 4.1 2.3 −39 288
002003 102 406 93 581 85 149 5 362 4 767 595 10 129 10.9 9.7 1.2 −9 534
002004 1 641 570 1 499 950 1 373 161 68 169 59 068 9 101 127 237 8.7 7.5 1.2 −118 136
002005 96 734 88 553 77 081 7 093 3 704 3 389 10 797 15.3 8.0 7.3 −7 408
003001 72 564 66 805 61 838 4 122 2 688 1 434 6 810 11.8 7.7 4.1 −5 376
003002 60 171 54 453 47 231 4 952 4 515 437 9 467 17.3 15.8 1.5 −9 030
003003 272 711 250 616 230 464 16 599 9 160 7 439 25 759 12.7 7.0 5.7 −18 320
003008 287 671 259 739 217 850 29 358 14 260 15 098 43 618 21.5 10.4 11.0 −28 520
Fuente: Estimaciones del CONAPO.

Migración Intramunicipal

Matrices

#Clave de los municipios 2015 
municipios <- MUN %>%
               select(CVE_MUN) %>%
                unique() %>%
                 pull(CVE_MUN)

Pob.5ymas <- mydata %>%
              mutate(CVE_MUN_RES = paste0(ENT_PAIS_RES10, MUN_RES10)) %>%
               mutate(ENT_PAIS_RES10 = case_when(.$ENT_PAIS_RES10 %in% estados ~.$ENT_PAIS_RES10,
                                                 .$ENT_PAIS_RES10 %nin% estados ~ "888", #Residencia en otro país
                                                 .$ENT_PAIS_RES10 %in% "997" ~ "997",
                                                 .$ENT_PAIS_RES10 %in% "998" ~ "998",
                                                 .$ENT_PAIS_RES10 %in% "997" ~ "999"),
                      CVE_MUN_RES = case_when(.$CVE_MUN_RES %in% municipios ~.$CVE_MUN_RES,
                                               nchar(.$CVE_MUN_RES) == 3 ~ "888",  #Residencia en otro país
                                              .$CVE_MUN_RES %in% "997999" ~ '997999',
                                              .$ENT_PAIS_RES10 %in% "997" ~ "997",
                                              .$ENT_PAIS_RES10 %in% "998" ~ "998",
                                              .$ENT_PAIS_RES10 %in% "999" ~ "999",
                                              .$MUN_RES10 %in% "999" ~ "999")) %>%
                 mutate(I_Migracion = case_when(.$CVE_ENT == .$ENT_PAIS_RES10 & .$ENT_PAIS_RES10 %in% estados ~ 1,
                                                .$CVE_ENT != .$ENT_PAIS_RES10 & .$ENT_PAIS_RES10 %in% estados ~ 2,
                                                .$ENT_PAIS_RES10 %nin% estados ~ 3)) %>%
                  mutate(EDAD = as.numeric(.$EDAD)) %>%
                   subset(EDAD >= 5 & EDAD <= 130) %>%
                    select(FACTOR, ESTRATO, UPM, CVE_MUN, CVE_MUN_RES, EDAD, I_Migracion) %>%
                     filter(CVE_MUN_RES %in% municipios & I_Migracion == 1) 

MC <- Pob.5ymas %>%
       svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/MC_intramunicipal.RDS"))
MC <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/MC_intramunicipal.RDS"))

Migrantes <- svytable(~CVE_MUN_RES + CVE_MUN, design = MC) 

Se genera la matriz cuadrada y se le asignan las etiquetas de municipios.

Migrantes <- Migrantes %>%
              as.data.frame() %>%
               expss::cross_cases(CVE_MUN, CVE_MUN_RES, weight = Freq) %>%
                as.data.frame() %>%
                 rename("CVE_MUN" = "row_labels") %>% 
                  arrange(CVE_MUN) %>%
                   slice(-1) 
            
rownames <- Migrantes %>% 
             mutate(CVE_MUN = substr(.$CVE_MUN, 9, 16)) %>% 
              pull(CVE_MUN)

colnames <- names(Migrantes) %>% 
             as.data.frame() %>% 
              slice(-1) %>% 
               rename("CVE_MUN" = ".") %>%
                mutate(`CVE_MUN` = substr(.$CVE_MUN, 13, 20)) %>%
                 pull(CVE_MUN)

# Se elimina la variable CVE_MUN
Migrantes <- Migrantes %>%
              select(-CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

saveRDS(Migrantes, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Matriz de migracion reciente a nivel intramunicipal 2015.RDS"))
save(Migrantes, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Matriz de migracion reciente a nivel intramunicipal 2015.RData"))

require(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "M.Intramunicipal")
writeData(wb, 1, Migrantes %>% as.data.frame() %>% tibble::rownames_to_column(var = "CVE_MUN"), colNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2015/Matriz de migracion reciente a nivel intramunicipal 2015.xlsx"), overwrite = TRUE)

Matriz de migración reciente hace 5 años a nivel municipal, 2010 - 2015

Matriz de migración reciente a nivel municipal
Nivel intramunicipal
CVE_MUN 001001 001002 001003 001004 001005 001006 001007 001008 001009 001010 001011 002001 002002 002003 002004 002005 003001 003002 003003 003008 003009 004001 004002 004003 004004 004005 004006 004007 004008
001001 752398 1045 525 90 991 105 142 26 76 237 448 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001002 221 39638 3 3 12 88 9 0 40 4 57 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001003 157 0 47892 5 16 12 11 0 0 0 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001004 73 2 4 13268 24 0 69 1 6 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001005 6154 43 52 115 94341 76 136 34 0 0 829 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001006 274 46 6 18 78 39191 161 58 135 0 369 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001007 84 43 20 136 19 143 45817 9 191 0 78 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001008 87 1 5 2 5 20 49 7407 1 0 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001009 70 61 0 2 13 77 171 1 17496 0 53 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001010 224 74 0 0 2 0 4 0 2 17285 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001011 3910 116 24 54 792 581 177 9 61 32 32515 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
002001 0 0 0 0 0 0 0 0 0 0 0 412427 964 353 1437 52 0 0 0 0 0 0 0 0 0 0 0 0 0
002002 0 0 0 0 0 0 0 0 0 0 0 1560 859462 343 1801 171 0 0 0 0 0 0 0 0 0 0 0 0 0
002003 0 0 0 0 0 0 0 0 0 0 0 177 543 85149 657 213 0 0 0 0 0 0 0 0 0 0 0 0 0
002004 0 0 0 0 0 0 0 0 0 0 0 1623 1620 1658 1373161 987 0 0 0 0 0 0 0 0 0 0 0 0 0
002005 0 0 0 0 0 0 0 0 0 0 0 264 350 130 2819 77081 0 0 0 0 0 0 0 0 0 0 0 0 0
003001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 61838 244 1019 633 357 0 0 0 0 0 0 0 0
003002 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 73 47231 358 87 107 0 0 0 0 0 0 0 0
003003 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1266 1440 230464 2758 328 0 0 0 0 0 0 0 0
003008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 663 114 1336 217850 99 0 0 0 0 0 0 0 0
003009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 136 86 202 148 15349 0 0 0 0 0 0 0 0
004001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 50623 139 61 59 24 8 0 4
004002 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 278 244921 749 904 125 537 86 38
004003 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 204 550 198216 288 107 3 153 11
004004 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 73 662 386 75323 17 23 31 0
004005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 28 151 47 38 27235 54 3 17
004006 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 42 273 39 18 23 34135 0 3
004007 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 22 107 3 2 8 7757 1
004008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 140 9 21 14 4 10 9316
004009 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 46 350 307 130 20 12 11 6
Fuente: Estimaciones del CONAPO.

Gráficos

ChordDiagram

Gráficos por estados

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

# Matriz cuadrada a nivel municipal 
load(paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Matriz de migracion reciente a nivel intramunicipal 2015.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

# Nombre de los estados 
estado <- stringr::str_wrap(nom_estados, 100)

# Clave de los municipios 
municipios <- MUN %>% 
               mutate(municipios = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                pull(municipios)

################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(municipios, Migrantes)

Emigrantes <- Emigrantes_function(municipios, Migrantes)   

### Sacar el promedio de los flujos migratiorios para determinar como se van a grupar los estados 
#### Es importante correr la tabla1[[x]] sin filtros para determinar el número promedio de flujos de migración
#### Filtro <<<<< filter(value > 0 & rn != estado[x]) %>%
#p <- data.frame(estados = est,
 #               filtro_municipio = tabla_municipios)
#write.table(p, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2015/Filtro a nivel intramunicipal.txt"), col.names = TRUE)
#write.xlsx(p, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2015/Filtro a nivel intramunicipal.xlsx"), overwrite = TRUE)

#### Filtro de municipios
filtro_mig <- read.xlsx(paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2015/Filtro a nivel intramunicipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_municipio)

################################################################################
## Se generan los filtros correspondientes a la matriz cuadrada por estados 
tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character) %>%
              filter(value > 0)

tabla1 <- migration_flows_municipality(tabla = tabla, 
                                       filtro_mun = filtro,
                                       filtro_est = NULL,
                                       filtro_mig = filtro_mig, 
                                       filtro_out = NULL,
                                       category_group = estados, 
                                       category_names = nom_estados,
                                       group_mun = "Otros municipios",
                                       group_est = NULL)

################################################################################
## Se generan los filtros correspondientes a la matriz cuadrada por estados 
## Se sacan los flujos migratorios que pertencen a otros municipios
#tabla_municipios <- sapply(1:32, function(i){
#                                  tabla1[[i]] %>%
#                                   as.data.frame() %>%
#                                    adorn_totals(c("row", "col"), 
#                                                  fill = "-", 
#                                                   na.rm = TRUE, 
#                                                    ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                                    select(Total) %>%
#                                     slice(nrow(.)) %>%
#                                      mutate(Total = .$Total/100) %>%
#                                       pull(Total)
#}
#)

## Se guardan las matrices de movilidad migración reciente para analizarlos después. 
wb <- createWorkbook()
for(i in 1:32){
     tabla <- tabla1[[i]] %>%
                as.data.frame() %>%
                 adorn_totals(c("row", "col"), 
                               fill = "-", 
                                na.rm = TRUE, 
          ,,,,contains(colnames(tabla1[[i]])))
                 
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, tabla, colNames = TRUE, rowNames = TRUE)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Matriz MR5a nivel intramunicipal_Reduccion.xlsx"), 
               overwrite = TRUE)
}
 
saveRDS(tabla1, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Tabla MR5a a nivel intramunicipal.RDS"))
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Tabla MR5a a nivel intramunicipal.RDS"))

total_tablas <- totales(tabla1 = tabla1, 
                        Clave = "CVE_MUN", 
                        Inmigrantes = "Inmigrantes",  
                        Emigrantes = "Emigrantes")

porcentajes_tablas <- porcentajes(tabla1 = tabla1, 
                                  Clave = "CVE_MUN", 
                                  Inmigrantes = "%Inmigrantes",  
                                  Emigrantes = "%Emigrantes")

# Se guardan los totales de las matrices reducidas 
wb <- createWorkbook()
for(i in 1:32){
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, totales_tablas[[i]], colNames = TRUE, startCol = 1)
     writeData(wb, i, porcentajes_tablas[[i]], colNames = TRUE, startCol = 5)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Matriz MR5a nivel intramunicipal_Reduccion_Totales.xlsx"), 
               overwrite = TRUE)
}

Dada la magnitud de municipios en algunos estados se seleccionan solo algunos de ellos.

tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Tabla MR5a a nivel intramunicipal.RDS"))

## Paleta de colores
#paleta <- colorRampPalette(pals::kovesi.linear_bmy_10_95_c78(100))(50)
paleta <- c("#000C7D", "#00108D", "#02149C", "#1614A4", "#3012A6", "#5A0D9D", "#7A0895", "#910390", "#A7008A", "#BB0085", "#CE0080", "#DF047A", "#EE1774", "#FA2C6C", "#FD4364", "#FE595B", "#FF6F51", "#FF8445","#FF9636", "#FFA72B", "#FFB622")

tabla2 <- color_chord_diagram(tabla1 = tabla1, paleta)
file = "/Graficos/Municipio/01_Migracion reciente/2015/ChordDiagram de MR5a desagregado a nivel intramunicipal.pdf"

## Gráficos a nivel intramunicipal
chord_diagram_graph(file = file, 
                    width = 15, 
                    height = 10, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#000C7D",
                    transparency = 0,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text =c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Etiquetas

file = "/Graficos/Municipio/01_Migracion reciente/2015/Etiquetas a nivel intramunicipal.pdf"

labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 8, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = nom_estados)

Gráfico Sankey

# Matriz cuadrada a nivel municipal 
load(paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Matriz de migracion reciente a nivel intramunicipal 2015.RData"))

tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character)

################################################################################
################################## Filtro ######################################
Inmigrantes  <- Migrantes %>%
                 as.data.frame() %>%
                  tibble::rownames_to_column(var = "rn") %>% 
                   melt(., id.vars = "rn", variable.name = "cn") %>%
                    mutate_if(is.factor, as.character) %>%
                     mutate(value = ifelse((.$rn != .$cn) & (substr(.$rn, 1, 3) %in% estados | substr(.$cn, 1, 3) %in% estados), value, 0)) %>%
                      filter(value > 0) %>%
                       group_by(rn) %>% 
                        summarise(Inmigrantes = sum(value, na.rm = TRUE)) 

Emigrantes <- Migrantes %>%
               as.data.frame() %>%
                tibble::rownames_to_column(var = "rn") %>% 
                 melt(., id.vars = "rn", variable.name = "cn") %>%
                  mutate_if(is.factor, as.character) %>%
                   mutate(value = ifelse((.$rn != .$cn) & (substr(.$rn, 1, 3) %in% estados | substr(.$cn, 1, 3) %in% estados), value, 0)) %>%
                    filter(value > 0) %>%
                     group_by(cn) %>% 
                      summarise(Emigrantes = sum(value, na.rm = TRUE))     

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
              filter(value < 5000) %>% 
               pull(rn)
################################################################################

tabla1 <- lapply(1:32, function(x){
                   tabla %>%
                    mutate(rn = case_when(substr(.$rn, 1, 3) %in% estados[x] & .$rn %in% filtro ~ paste0("Otros municipios (", estados[x], ")"),
                                          substr(.$rn, 1, 3) %in% estados[x]  & .$rn %nin% filtro ~ .$rn,
                                          substr(.$rn, 1, 3) %nin% estados[x] ~ substr(.$rn, 1, 3)),
                           cn = case_when(substr(.$cn, 1, 3) %in% estados[x] & .$cn %in% filtro ~ paste0("Otros municipios (", estados[x], ")"),
                                          substr(.$cn, 1, 3) %in% estados[x] & .$cn %nin% filtro ~ .$cn,
                                          substr(.$cn, 1, 3) %nin% estados[x] ~ substr(.$cn, 1, 3))) %>%

                     mutate(value = ifelse(.$rn != .$cn, .$value, 0)) %>%
                      filter(value > 0) 
  }
)
p <- lapply(1:32, function(x){
             tabla1[[x]] %>% 
              ggplot(aes(axis1 = rn, 
                          axis2 = cn, 
                           y = value),  # c("value", "freq", "tasa")
                      reverse = FALSE, 
                       na.rm = TRUE) +
               geom_alluvium(aes(fill = rn),
                              curve_type = "quintic", 
                               color = "transparent", 
                                alpha = 0.85, 
                                 lwd = 0.001, 
                                  width = 1/5,
                                   reverse = FALSE) +
                 geom_stratum(aes(fill = cn), 
                               color = "white", 
                                alpha = 0.65,  
                                 lwd = 0.001, 
                                  width = 1/5,
                                   reverse = FALSE) +
                  geom_text_repel(aes(label = ifelse(after_stat(x) == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""), 
                                       fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                                   stat = "stratum", 
                                    size = 3, 
                                     direction = "y", 
                                      nudge_x = -.2,
                                       min.segment.length = unit(1, "lines"),
                                        force = 1,
                                         force_pull = 0,
                                          family = "montserrat",
                                           reverse = FALSE) +
                   geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                        fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                                    stat = "stratum", 
                                     size = 3,
                                      direction = "y", 
                                       nudge_x = .2, 
                                        force = 1,
                                         force_pull = 0,
                                          family = "montserrat",
                                           reverse = FALSE) +
                     theme_void() + 
                      theme(plot.margin = margin(t = 1, r = 5, b = 1, l = 0, "cm"),
                             text = element_text(family = "montserrat"),
                              axis.text = element_blank(),
                               axis.title = element_blank(),
                                strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                                 legend.key.size = unit(0.5, "cm"),
                                  legend.text = element_text(size = 7, family = "montserrat"),
                                   legend.position = c(1.02, .5)) + 
                       scale_x_discrete(expand = c(-0.1, 0.5)) +
                        scale_fill_viridis_d(option = "A", end = 1, begin = 0.2) +
                         guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                          labs(fill = "", 
                               color = "")
  }
)

path = paste0(here::here(), "/Graficos/Municipio/01_Migracion reciente/2015/GSankey de MR5a desagregado a nivel intramunicipal.pdf")
ggexport(list = p, width = 18, height = 10, dpi = 400, filename = path)

Indicadores

Se realizan cálculos generales de migración

  • Residentes

  • Inmigrantes

  • Emigrantes

  • % Inmigrantes

  • % Emigrante

  • Migración bruta

  • Migración Neta

  • % Tasa de migración bruta

  • % Tasa de migración neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_MUN) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
###################### Población de 5 años y más ###############################
Pob.5ymas <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset(EDAD >= 5 & EDAD <=130) %>%
                 group_by(CVE_MUN) %>%
                  summarise(Pob.5ymas = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Matriz de migracion reciente a nivel intramunicipal 2015.RData"))

Residentes <- Migrantes %>%
               rownames_to_column() %>%
                gather(CVE_MUN, Value, -rowname)%>%
                 filter(rowname == CVE_MUN) %>%
                  select(-rowname) %>%
                   droplevels() %>%
                    rename("Residentes" = "Value") 

################################################################################
############################### Inmigrantes ####################################
Inmigrantes <- Migrantes %>% 
                as.data.frame() %>%
                 tibble::rownames_to_column(var = "CVE_MUN") %>%
                  melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
                   mutate_at(vars(3), as.numeric) %>%
                    as_tibble() %>%
                     filter(CVE_MUN != CVE_MUN_RES) %>%
                      group_by(CVE_MUN) %>%
                       summarise(Inmigrantes = sum(value, na.rm = TRUE))

################################################################################
############################### Emigrantes #####################################
Emigrantes <- Migrantes %>% 
               as.data.frame() %>%
                tibble::rownames_to_column(var = "CVE_MUN") %>%
                 melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
                  mutate_at(vars(3), as.numeric) %>%
                   as_tibble() %>%
                    filter(CVE_MUN != CVE_MUN_RES) %>%
                     group_by(CVE_MUN_RES) %>%
                      summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
                       rename("CVE_MUN" = "CVE_MUN_RES") 

tabla <- Pob.Total %>%
          left_join(., Pob.5ymas, by = c("CVE_MUN")) %>%
          left_join(., Residentes, by = c("CVE_MUN")) %>%
          left_join(., Inmigrantes, by = c("CVE_MUN")) %>%
          left_join(., Emigrantes, by = c("CVE_MUN")) %>%
           mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                  Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                  Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
                  Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
                  Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                  Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Indicadores de MR5a a nivel intramunicipal 2015.xlsx"), overwrite = TRUE)
save(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Indicadores de MR5a a nivel intramunicipal 2015.RData"))
Indicadores de migración reciente
Nivel intramunicipal
CVE_MUN Pob.Total Pob.5ymas Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
001001 877 190 797 086 752 398 3 685 11 254 −7 569 14 939 0.88 2.69 −1.8 −22 508
001002 46 464 41 358 39 638 437 1 431 −994 1 868 1.99 6.52 −4.5 −2 862
001003 56 048 50 160 47 892 213 639 −426 852 0.80 2.41 −1.6 −1 278
001004 15 577 13 875 13 268 188 425 −237 613 2.55 5.77 −3.2 −850
001005 120 405 107 447 94 341 7 439 1 952 5 487 9 391 13.06 3.43 9.6 −3 904
001006 46 473 41 391 39 191 1 145 1 102 43 2 247 5.21 5.02 0.2 −2 204
001007 53 866 47 805 45 817 723 929 −206 1 652 2.84 3.65 −0.8 −1 858
001008 8 896 7 811 7 407 194 138 56 332 4.64 3.30 1.3 −276
001009 20 926 18 568 17 496 448 512 −64 960 4.54 5.19 −0.6 −1 024
001010 20 245 18 101 17 285 318 273 45 591 3.32 2.85 0.5 −546
001011 46 454 40 814 32 515 5 756 1 891 3 865 7 647 26.38 8.67 17.7 −3 782
002001 486 639 444 765 412 427 2 806 3 624 −818 6 430 1.21 1.56 −0.4 −7 248
002002 988 417 906 626 859 462 3 875 3 477 398 7 352 0.82 0.73 0.1 −6 954
002003 102 406 93 581 85 149 1 590 2 484 −894 4 074 3.25 5.07 −1.8 −4 968
002004 1 641 570 1 499 950 1 373 161 5 888 6 714 −826 12 602 0.75 0.85 −0.1 −13 428
002005 96 734 88 553 77 081 3 563 1 423 2 140 4 986 7.69 3.07 4.6 −2 846
003001 72 564 66 805 61 838 2 253 2 138 115 4 391 6.47 6.14 0.3 −4 276
003002 60 171 54 453 47 231 625 1 884 −1 259 2 509 2.18 6.57 −4.4 −3 768
003003 272 711 250 616 230 464 5 792 2 915 2 877 8 707 4.43 2.23 2.2 −5 830
003008 287 671 259 739 217 850 2 212 3 626 −1 414 5 838 1.62 2.65 −1.0 −7 252
Fuente: Estimaciones del CONAPO.

Migración Intermunicipal

Matrices

#Clave de los municipios 2015 
municipios <- MUN %>%
               select(CVE_MUN) %>%
                unique() %>%
                 pull(CVE_MUN)

Pob.5ymas <- mydata %>%
              mutate(CVE_MUN_RES = paste0(ENT_PAIS_RES10, MUN_RES10)) %>%
               mutate(ENT_PAIS_RES10 = case_when(.$ENT_PAIS_RES10 %in% estados ~.$ENT_PAIS_RES10,
                                                 .$ENT_PAIS_RES10 %nin% estados ~ "888", #Residencia en otro país
                                                 .$ENT_PAIS_RES10 %in% "997" ~ "997",
                                                 .$ENT_PAIS_RES10 %in% "998" ~ "998",
                                                 .$ENT_PAIS_RES10 %in% "997" ~ "999"),
                      CVE_MUN_RES = case_when(.$CVE_MUN_RES %in% municipios ~.$CVE_MUN_RES,
                                               nchar(.$CVE_MUN_RES) == 3 ~ "888",  #Residencia en otro país
                                              .$CVE_MUN_RES %in% "997999" ~ '997999',
                                              .$ENT_PAIS_RES10 %in% "997" ~ "997",
                                              .$ENT_PAIS_RES10 %in% "998" ~ "998",
                                              .$ENT_PAIS_RES10 %in% "999" ~ "999",
                                              .$MUN_RES10 %in% "999" ~ "999")) %>%
                 mutate(I_Migracion = case_when(.$CVE_ENT == .$ENT_PAIS_RES10 & .$ENT_PAIS_RES10 %in% estados ~ 1,
                                                .$CVE_ENT != .$ENT_PAIS_RES10 & .$ENT_PAIS_RES10 %in% estados ~ 2,
                                                .$ENT_PAIS_RES10 %nin% estados ~ 3)) %>%
                  mutate(EDAD = as.numeric(.$EDAD)) %>%
                   subset(EDAD >= 5 & EDAD <= 130) %>%
                    select(FACTOR, ESTRATO, UPM, CVE_MUN, CVE_MUN_RES, EDAD, I_Migracion) %>%
                     filter(CVE_MUN_RES %in% municipios & I_Migracion == 2) 

MC <- Pob.5ymas %>%
       svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)

saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/MC_intermunicipal.RDS"))
MC <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/MC_intermunicipal.RDS"))

Migrantes <- svytable(~CVE_MUN_RES + CVE_MUN, design = MC) 

Se genera la matriz cuadrada y se le asignan las etiquetas de municipios.

Migrantes <- Migrantes %>%
              as.data.frame() %>%
               expss::cross_cases(CVE_MUN, CVE_MUN_RES, weight = Freq) %>%
                as.data.frame() %>%
                 rename("CVE_MUN" = "row_labels") %>% 
                  arrange(CVE_MUN) %>%
                   slice(-1) 
            
rownames <- Migrantes %>% 
             mutate(CVE_MUN = substr(.$CVE_MUN, 9, 16)) %>% 
              pull(CVE_MUN)

colnames <- names(Migrantes) %>% 
             as.data.frame() %>% 
              slice(-1) %>% 
               rename("CVE_MUN" = ".") %>%
                mutate(`CVE_MUN` = substr(.$CVE_MUN, 13, 20)) %>%
                 pull(CVE_MUN)

# Se elimina la variable CVE_MUN
Migrantes <- Migrantes %>%
              select(-CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

saveRDS(Migrantes, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Matriz de migracion reciente a nivel intermunicipal 2015.RDS"))
save(Migrantes, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Matriz de migracion reciente a nivel intermunicipal 2015.RData"))

require(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "M.Intermunicipal")
writeData(wb, 1, Migrantes %>% as.data.frame() %>% tibble::rownames_to_column(var = "CVE_MUN"), colNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2015/Matriz de migracion reciente a nivel intermunicipal 2015.xlsx"), overwrite = TRUE)

Matriz de migración reciente hace 5 años a nivel municipal, 2010 - 2015

Matriz de migración reciente a nivel municipal
Nivel intermunicipal
CVE_MUN 001001 001002 001003 001004 001005 001006 001007 001008 001009 001010 001011 002001 002002 002003 002004 002005 003001 003002 003003 003008 003009 004001 004002 004003 004004 004005 004006 004007 004008
001001 0 0 0 0 0 0 0 0 0 0 0 45 69 0 213 0 0 0 0 86 0 0 20 0 0 0 0 0 0
001002 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001003 0 0 0 0 0 0 0 0 0 0 0 5 0 8 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001004 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001005 0 0 0 0 0 0 0 0 0 0 0 0 60 0 25 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001006 0 0 0 0 0 0 0 0 0 0 0 0 3 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001007 0 0 0 0 0 0 0 0 0 0 0 0 4 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001009 0 0 0 0 0 0 0 0 0 0 0 0 0 2 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0
001010 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 2 0 0 0 0 0 0 0 0 0
001011 0 0 0 0 0 0 0 0 0 0 0 4 0 0 7 0 0 0 3 0 0 0 0 0 0 0 0 0 0
002001 35 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 163 1573 489 251 0 0 0 32 0 0 0 0 0
002002 130 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 98 106 183 190 0 0 0 0 0 0 0 0 0
002003 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 18 24 22 0 0 0 0 0 0 0 0 0
002004 660 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 80 25 589 289 0 0 0 0 0 0 0 0 0
002005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 49 22 0 0 0 0 0 0 0 0 0
003001 0 0 0 0 0 0 0 0 0 0 0 132 94 0 205 12 0 0 0 0 0 0 0 0 0 0 0 0 0
003002 0 0 0 0 0 0 0 0 0 0 0 787 88 5 168 35 0 0 0 0 0 0 0 0 0 0 0 0 0
003003 0 0 0 0 0 0 0 0 0 0 0 553 133 43 211 60 0 0 0 0 0 0 4 0 0 0 0 0 0
003008 5 0 0 0 0 0 0 0 0 0 0 266 297 6 587 730 0 0 0 0 0 0 6 0 0 0 0 0 0
003009 0 0 0 0 0 0 0 0 0 0 0 66 24 0 30 0 0 0 0 0 0 0 0 2 0 0 0 0 0
004001 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004002 4 0 0 0 0 0 0 0 0 0 0 5 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004003 0 0 0 0 0 0 0 0 0 0 0 308 0 39 46 0 0 0 0 9 0 0 0 0 0 0 0 0 0
004004 0 0 0 0 0 0 0 0 0 0 0 21 6 0 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004006 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004007 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
004008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 4 0 0 0 0 0 0 0 0 0 0 0 0 0
004009 0 0 0 0 0 0 0 0 0 0 0 0 1 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Fuente: Estimaciones del CONAPO.

Gráficos

ChordDiagram

Gráficos por estados

Se filtran los flujos migratorios que son exclusivos de los estados y que visualmente sean más interpretables.

# Matriz cuadrada a nivel municipal 
load(paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Matriz de migracion reciente a nivel intermunicipal 2015.RData"))

rownames <- rownames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

colnames <- colnames(Migrantes) %>% 
             as.data.frame() %>%
              rename("CVE_MUN" = ".") %>%
               left_join(., MUN %>% select(CVE_MUN, NOM_MUN)) %>%
                mutate(CVE_MUN = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                 pull(CVE_MUN)

rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames

# Nombre de los estados 
estado <- stringr::str_wrap(nom_estados, 100)

# Clave de los municipios 
municipios <- MUN %>% 
               mutate(municipios = stringr::str_wrap(paste(.$CVE_MUN, .$NOM_MUN), 100)) %>%
                pull(municipios)
             
################################################################################
################################## Filtro ######################################
Inmigrantes <- Inmigrantes_function(municipios, Migrantes)

Emigrantes <- Emigrantes_function(municipios, Migrantes)  

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
              filter(value > 0)

filtro_est <- Migrantes %>%
               as.data.frame() %>%
                tibble::rownames_to_column(var = "rn") %>% 
                 melt(., id.vars = "rn", variable.name = "cn") %>%
                  mutate_if(is.factor, as.character) %>%
                   filter(value > 0)

### Sacar el promedio de los flujos migratiorios para determinar como se van a grupar los estados 
#### Es importante correr la tabla1[[x]] sin filtros para determinar el número promedio de flujos de migración
#### Filtro <<<<< filter(value > 0 & rn != estado[x]) %>%
#p <- data.frame(estados = est,
 #               filtro_municipio = tabla_municipios,
  #              filtro_estado = tabla_estados)
#write.xlsx(p, file = paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2015/Filtro a nivel intermunicipal.xlsx"), overwrite = TRUE)

#### Filtro de municipios
filtro_mig <- read.xlsx(paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2015/Filtro a nivel intermunicipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_municipio)

#### Filtro de estados 
filtro_out <- read.xlsx(paste0(here::here(), "/Bases/Municipio/01_Migracion reciente/2015/Filtro a nivel intermunicipal.xlsx"), colNames = TRUE) %>%
               pull(filtro_estado)

################################################################################
## Se generan los filtros correspondientes a la matriz cuadrada por estados 
tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character) %>%
              filter(value > 0)

tabla1 <- migration_flows_municipality(tabla = tabla, 
                                       filtro_mun = filtro,
                                       filtro_est = filtro_est,
                                       filtro_mig = filtro_mig, 
                                       filtro_out = filtro_out, 
                                       category_group = estados, 
                                       category_names = nom_estados,
                                       group_mun = "Otros municipios",
                                       group_est = "Otros estados")

## Se sacan los flujos migratorios que pertencen a otros estados
#tabla_estados <- sapply(1:32, function(i){
#                                 tabla1[[i]] %>%
#                                  as.data.frame() %>%
#                                   adorn_totals(c("row", "col"), 
#                                                 fill = "-", 
#                                                  na.rm = TRUE, 
#                                                   ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                                    select(`Otros estados`) %>%
#                                     slice(nrow(.)) %>%
#                                      mutate(`Otros estados` = .$`Otros estados`/10) %>%
#                                       pull(`Otros estados`)
#})

## Se sacan los flujos migratorios que pertencen a otros municipios
#tabla_municipios <- sapply(1:32, function(i){
#                               tabla1[[i]] %>%
#                                as.data.frame() %>%
#                                 select(-c(`Otros estados`)) %>%
#                                  adorn_totals(c("row", "col"), 
#                                                fill = "-", 
#                                                 na.rm = TRUE, 
#                                                  ,,,,contains(colnames(tabla1[[i]]))) %>% 
#                                    slice(nrow(.)) %>%
#                                     mutate(Total = .$Total/50) %>%
#                                      pull(Total)
#})
## Se guardan las matrices de movilidad migración reciente para analizarlos después. 
wb <- createWorkbook()
for(i in 1:32){
     tabla <- tabla1[[i]] %>%
                as.data.frame() %>%
                 adorn_totals(c("row", "col"), 
                               fill = "-", 
                                na.rm = TRUE, 
          ,,,,contains(colnames(tabla1[[i]])))
                 
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, tabla, colNames = TRUE, rowNames = TRUE)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Matriz MR5a a nivel intermunicipal_Reduccion.xlsx"), 
               overwrite = TRUE)
}

saveRDS(tabla1, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Tabla MR5a a nivel intermunicipal.RDS"))
tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Tabla MR5a a nivel intermunicipal.RDS"))

total_tablas <- totales(tabla1 = tabla1, 
                        Clave = "CVE_MUN", 
                        Inmigrantes = "Inmigrantes",  
                        Emigrantes = "Emigrantes")

porcentajes_tablas <- porcentajes(tabla1 = tabla1, 
                                  Clave = "CVE_MUN", 
                                  Inmigrantes = "%Inmigrantes",  
                                  Emigrantes = "%Emigrantes")

# Se guardan los totales de las matrices reducidas 
wb <- createWorkbook()
for(i in 1:32){
     addWorksheet(wb, paste(est[i]))
     writeData(wb, i, totales_tablas[[i]], colNames = TRUE, startCol = 1)
     writeData(wb, i, porcentajes_tablas[[i]], colNames = TRUE, startCol = 5)
     saveWorkbook(wb, 
                  file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Matriz MR5a a nivel intermunicipal_Reduccion_Totales.xlsx"), 
               overwrite = TRUE)
}

Dada la magnitud de municipios en algunos estados se seleccionan solo algunos de ellos.

tabla1 <- readRDS(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Tabla MR5a a nivel intermunicipal.RDS"))

## Paleta de colores
#paleta <- colorRampPalette(pals::kovesi.linear_bmy_10_95_c78(100))(50)
paleta <- c("#000C7D", "#00108D", "#02149C", "#1614A4", "#3012A6", "#5A0D9D", "#7A0895", "#910390", "#A7008A", "#BB0085", "#CE0080", "#DF047A", "#EE1774", "#FA2C6C", "#FD4364", "#FE595B", "#FF6F51", "#FF8445","#FF9636", "#FFA72B", "#FFB622")

tabla2 <- color_chord_diagram(tabla1 = tabla1, paleta)
file = "/Graficos/Municipio/01_Migracion reciente/2015/ChordDiagram de MR5a desagregado a nivel intermunicipal.pdf"

## Gráficos a nivel intermunicipal
chord_diagram_graph(file = file, 
                    width = 15, 
                    height = 10, 
                    family = "Montserrat Medium", 
                    paleta = paleta, 
                    tabla1 = tabla1, 
                    tabla2 = tabla2, 
                    color_labels = "#000C7D",
                    transparency = 0,
                    circo.text = 9,
                    circos.axis.text = 6,
                    adj.text =c(-0.05, 0.5), #Ajuste de las etiquetas (x, y)
                    adj.ylim = 0.1,
                    gap.degree = 2, 
                    clock.wise = FALSE,
                    track.margin = c(-0.07, 0.1),
                    margin = rep(0, 4))

Etiquetas

file = "/Graficos/Municipio/01_Migracion reciente/2015/Etiquetas a nivel intermunicipal.pdf"

labels_chord_diagram(file = file, 
                     width = 7, 
                     height = 8, 
                     family = "Montserrat Medium", 
                     paleta = paleta, 
                     tabla1 = tabla1, 
                     labels = nom_estados)

Gráfico Sankey

# Matriz cuadrada a nivel municipal 
load(paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Matriz de migracion reciente a nivel intermunicipal 2015.RData"))

tabla <- Migrantes %>%
          as.data.frame() %>%
           tibble::rownames_to_column(var = "rn") %>%
            melt(., id.vars = "rn", variable.name = "cn") %>%
             mutate_if(is.factor, as.character)

################################################################################
################################## Filtro ######################################
Inmigrantes  <- Migrantes %>%
                 as.data.frame() %>%
                  tibble::rownames_to_column(var = "rn") %>% 
                   melt(., id.vars = "rn", variable.name = "cn") %>%
                    mutate_if(is.factor, as.character) %>%
                     mutate(value = ifelse((.$rn != .$cn) & (substr(.$rn, 1, 3) %in% estados | substr(.$cn, 1, 3) %in% estados), value, 0)) %>%
                      filter(value > 0) %>%
                       group_by(rn) %>% 
                        summarise(Inmigrantes = sum(value, na.rm = TRUE)) 

Emigrantes <- Migrantes %>%
               as.data.frame() %>%
                tibble::rownames_to_column(var = "rn") %>% 
                 melt(., id.vars = "rn", variable.name = "cn") %>%
                  mutate_if(is.factor, as.character) %>%
                   mutate(value = ifelse((.$rn != .$cn) & (substr(.$rn, 1, 3) %in% estados | substr(.$cn, 1, 3) %in% estados), value, 0)) %>%
                    filter(value > 0) %>%
                     group_by(cn) %>% 
                      summarise(Emigrantes = sum(value, na.rm = TRUE))     

################################## Filtro ######################################
filtro  <- Inmigrantes %>%
            full_join(., Emigrantes, by = c("rn" = "cn")) %>%
             mutate(value = sum_row(Inmigrantes, Emigrantes, na.rm = TRUE)) %>%
              filter(value < 5000) %>% 
               pull(rn)
################################################################################

tabla1 <-  lapply(1:32, function(x){
                   tabla %>%
                    mutate(rn = case_when(substr(.$rn, 1, 3) %in% estados[x] & .$rn %in% filtro ~ paste0(estados[x], " Otros municipios (", estados[x], ")"),
                                          substr(.$rn, 1, 3) %in% estados[x]  & .$rn %nin% filtro ~ .$rn,
                                          substr(.$rn, 1, 3) %nin% estados[x] ~ paste0(nom_estados[as.numeric(substr(.$rn, 1, 3))])),
                           cn = case_when(substr(.$cn, 1, 3) %in% estados[x] & .$cn %in% filtro ~ paste0(estados[x], " Otros municipios (", estados[x], ")"),
                                          substr(.$cn, 1, 3) %in% estados[x] & .$cn %nin% filtro ~ .$cn,
                                          substr(.$cn, 1, 3) %nin% estados[x] ~ paste0(nom_estados[as.numeric(substr(.$cn, 1, 3))]))) %>%
                     mutate(value = ifelse(.$rn != .$cn & (substr(.$rn, 1, 3) %in% estados[x] | substr(.$cn, 1, 3) %in% estados[x]), .$value, 0)) %>%
                      filter(value > 0)  
  }
)
p <- lapply(1:32, function(x){
             tabla1[[x]] %>% 
              ggplot(aes(axis1 = rn, 
                          axis2 = cn, 
                           y = value),  # c("value", "freq", "tasa")
                      reverse = FALSE, 
                       na.rm = TRUE) +
               geom_alluvium(aes(fill = rn),
                              curve_type = "quintic", 
                               color = "transparent", 
                                alpha = 0.85, 
                                 lwd = 0.001, 
                                  width = 1/5,
                                   reverse = FALSE) +
                 geom_stratum(aes(fill = cn), 
                               color = "white", 
                                alpha = 0.65,  
                                 lwd = 0.001, 
                                  width = 1/5,
                                   reverse = FALSE) +
                  geom_text_repel(aes(label = ifelse(after_stat(x) == 1, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""), 
                                       fontface =  ifelse(after_stat(x) == 1, 'bold', 'plain')),
                                   stat = "stratum", 
                                    size = 3, 
                                     direction = "y", 
                                      nudge_x = -.2,
                                       min.segment.length = unit(1, "lines"),
                                        force = 1,
                                         force_pull = 0,
                                          family = "montserrat",
                                           reverse = FALSE) +
                   geom_text_repel(aes(label = ifelse(after_stat(x)  == 2, paste0(as.character(after_stat(stratum)),  ": ", prettyNum(count, big.mark = " ")), ""),
                                        fontface =  ifelse(after_stat(x) == 2, 'bold', 'plain')),
                                    stat = "stratum", 
                                     size = 3,
                                      direction = "y", 
                                       nudge_x = .2, 
                                        force = 1,
                                         force_pull = 0,
                                          family = "montserrat",
                                           reverse = FALSE) +
                     theme_void() + 
                      theme(plot.margin = margin(t = 1, r = 5, b = 1, l = 0, "cm"),
                             text = element_text(family = "montserrat"),
                              axis.text = element_blank(),
                               axis.title = element_blank(),
                                strip.text = element_text(size = 10, face = "bold", family = "montserrat"),
                                 legend.key.size = unit(0.5, "cm"),
                                  legend.text = element_text(size = 7, family = "montserrat"),
                                   legend.position = c(1.02, .5)) + 
                       scale_x_discrete(expand = c(-0.1, 0.5)) +
                        scale_fill_viridis_d(option = "A", end = 1, begin = 0.2) +
                         guides(fill = guide_legend(ncol = 1, na.translate = F)) + 
                          labs(fill = "", 
                               color = "")
  }
)

path = paste0(here::here(), "/Graficos/Municipio/01_Migracion reciente/2015/GSankey de MR5a desagregado a nivel intermunicipal.pdf")
ggexport(list = p, width = 18, height = 10, dpi = 400, filename = path)

Indicadores

Se realizan cálculos generales de migración

  • Residentes

  • Inmigrantes

  • Emigrantes

  • % Inmigrantes

  • % Emigrante

  • Migración bruta

  • Migración Neta

  • % Tasa de migración bruta

  • % Tasa de migración neta

Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora

################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
              as.data.frame() %>%
               group_by(CVE_MUN) %>%
                summarise(Pob.Total = sum(FACTOR)) 

################################################################################
###################### Población de 5 años y más ###############################
Pob.5ymas <- mydata %>%
              as.data.frame() %>%
               mutate(EDAD = as.numeric(.$EDAD)) %>%
                subset(EDAD >= 5 & EDAD <=130) %>%
                 group_by(CVE_MUN) %>%
                  summarise(Pob.5ymas = sum(FACTOR)) 

################################################################################
########################### Residentes #########################################
load(file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Matriz de migracion reciente a nivel intermunicipal 2015.RData"))

Residentes <- Migrantes %>%
               rownames_to_column() %>%
                gather(CVE_MUN, Value, -rowname)%>%
                 filter(rowname == CVE_MUN) %>%
                  select(-rowname) %>%
                   droplevels() %>%
                    rename("Residentes" = "Value") 

################################################################################
############################### Inmigrantes ####################################
Inmigrantes <- Migrantes %>% 
                as.data.frame() %>%
                 tibble::rownames_to_column(var = "CVE_MUN") %>%
                  melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
                   mutate_at(vars(3), as.numeric) %>%
                    as_tibble() %>%
                     filter(CVE_MUN != CVE_MUN_RES) %>%
                      group_by(CVE_MUN) %>%
                       summarise(Inmigrantes = sum(value, na.rm = TRUE))

################################################################################
############################### Emigrantes #####################################
Emigrantes <- Migrantes %>% 
               as.data.frame() %>%
                tibble::rownames_to_column(var = "CVE_MUN") %>%
                 melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
                  mutate_at(vars(3), as.numeric) %>%
                   as_tibble() %>%
                    filter(CVE_MUN != CVE_MUN_RES) %>%
                     group_by(CVE_MUN_RES) %>%
                      summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
                       rename("CVE_MUN" = "CVE_MUN_RES") 

tabla <- Pob.Total %>%
          left_join(., Pob.5ymas, by = c("CVE_MUN")) %>%
          left_join(., Residentes, by = c("CVE_MUN")) %>%
          left_join(., Inmigrantes, by = c("CVE_MUN")) %>%
          left_join(., Emigrantes, by = c("CVE_MUN")) %>%
           mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
                  Mig.Bruta = .$Inmigrantes + .$Emigrantes, 
                  Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
                  Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
                  Tasa.Mig = Tasa.Inmig - Tasa.Emig, 
                  Eficacia = Mig.Neta - Mig.Bruta)

write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Indicadores de MR5a a nivel intermunicipal 2015.xlsx"), overwrite = TRUE)
save(tabla, file = paste0(here::here(), "/Output/Municipio/01_Migracion reciente/2015/Indicadores de MR5a a nivel intermunicipal 2015.RData"))
Indicadores de migración reciente
Nivel intermunicipal
CVE_MUN Pob.Total Pob.5ymas Residentes Inmigrantes Emigrantes Mig.Neta Mig.Bruta Tasa.Inmig Tasa.Emig Tasa.Mig Eficacia
001001 877 190 797 086 0 25 580 18 671 6 909 44 251 6.1 4.46 1.7 −37 342
001002 46 464 41 358 0 666 181 485 847 3.0 0.82 2.2 −362
001003 56 048 50 160 0 550 408 142 958 2.1 1.54 0.5 −816
001004 15 577 13 875 0 239 111 128 350 3.2 1.51 1.7 −222
001005 120 405 107 447 0 3 545 206 3 339 3 751 6.2 0.36 5.9 −412
001006 46 473 41 391 0 517 98 419 615 2.4 0.45 1.9 −196
001007 53 866 47 805 0 494 294 200 788 1.9 1.16 0.8 −588
001008 8 896 7 811 0 50 110 −60 160 1.2 2.63 −1.4 −220
001009 20 926 18 568 0 303 28 275 331 3.1 0.28 2.8 −56
001010 20 245 18 101 0 264 33 231 297 2.8 0.34 2.4 −66
001011 46 454 40 814 0 1 636 124 1 512 1 760 7.5 0.57 6.9 −248
002001 486 639 444 765 0 18 852 12 294 6 558 31 146 8.1 5.28 2.8 −24 588
002002 988 417 906 626 0 26 567 16 167 10 400 42 734 5.6 3.41 2.2 −32 334
002003 102 406 93 581 0 3 772 2 283 1 489 6 055 7.7 4.66 3.0 −4 566
002004 1 641 570 1 499 950 0 62 281 52 354 9 927 114 635 7.9 6.67 1.3 −104 708
002005 96 734 88 553 0 3 530 2 281 1 249 5 811 7.6 4.92 2.7 −4 562
003001 72 564 66 805 0 1 869 550 1 319 2 419 5.4 1.58 3.8 −1 100
003002 60 171 54 453 0 4 327 2 631 1 696 6 958 15.1 9.18 5.9 −5 262
003003 272 711 250 616 0 10 807 6 245 4 562 17 052 8.3 4.77 3.5 −12 490
003008 287 671 259 739 0 27 146 10 634 16 512 37 780 19.8 7.77 12.1 −21 268
Fuente: Estimaciones del CONAPO.

Referencias

Librerias que se usaron en el documento

package loadedversion source
Cairo 1.6-1 CRAN (R 4.3.1)
chorddiag 0.1.3 Github ()
circlize 0.4.15 CRAN (R 4.3.1)
doMC 1.3.5 R-Forge (R 4.3.1)
dplyr 1.1.3 CRAN (R 4.3.2)
expss 0.11.6 CRAN (R 4.3.1)
foreach 1.5.2 CRAN (R 4.3.1)
ggalluvial 0.12.5 CRAN (R 4.3.1)
ggplot2 3.4.3 CRAN (R 4.3.1)
ggpubr 0.6.0 CRAN (R 4.3.1)
ggrepel 0.9.3 CRAN (R 4.3.1)
ggsankey 0.0.99999 Github ()
gt 0.10.0 CRAN (R 4.3.1)
haven 2.5.3 CRAN (R 4.3.1)
Hmisc 5.1-0 CRAN (R 4.3.1)
iterators 1.0.14 CRAN (R 4.3.1)
janitor 2.2.0 CRAN (R 4.3.1)
kableExtra 1.3.4 CRAN (R 4.3.1)
knitr 1.45 CRAN (R 4.3.2)
maditr 0.8.3 CRAN (R 4.3.1)
mapview 2.11.0 CRAN (R 4.3.1)
Matrix 1.6-1.1 CRAN (R 4.3.1)
network 1.18.1 CRAN (R 4.3.1)
openxlsx 4.2.5.2 CRAN (R 4.3.1)
reshape2 1.4.4 CRAN (R 4.3.1)
sjlabelled 1.2.0 CRAN (R 4.3.1)
sna 2.7-1 CRAN (R 4.3.1)
srvyr 1.2.0 CRAN (R 4.3.1)
statnet.common 4.9.0 CRAN (R 4.3.1)
stringr 1.5.0 CRAN (R 4.3.1)
survey 4.2 Github ()
survival 3.5-5 CRAN (R 4.3.1)
tibble 3.2.1 CRAN (R 4.3.1)
tidyr 1.3.0 CRAN (R 4.3.1)

Creative Commons Licence
This work by Diana Villasana Ocampo is licensed under a Creative Commons Attribution 4.0 International License.